/*
 ===============================================================================
 Name        : main.cpp
 Author      : $(user)
 Version     : 1.0.0
 Date		 : $(date)
 Copyright   :
 License	 : CC BY-SA 3.0
 Description :
 ===============================================================================
  	 	 	 	 	 	 	 	 History
 ---------+---------+--------------------------------------------+-------------
 DATE     |	VERSION |	DESCRIPTIONS							 |	By
 ---------+---------+--------------------------------------------+-------------

 ===============================================================================
 */
#include "driver_config.h"
#ifdef __USE_CMSIS
#include "LPC12xx.h"
#endif

#include <cr_section_macros.h>
#include <NXP/crp.h>

// Variable to store CRP value in. Will be placed automatically
// by the linker when "Enable Code Read Protect" selected.
// See crp.h header for more information
__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;

// TODO: insert other include files here
#include "lpc12xx_sysctrl.h"
#include "lpc12xx_iocon.h"
#include "lpc12xx_gpio.h"
#include "rtos.h"

// LPCXpresso processor card LED
#define LED1_PORT 	LPC_GPIO0		// Port for LED1
#define LED1_BIT 	7				// Bit on port for LED1
#define LED2_PORT	LPC_GPIO0		// Port for led2
#define LED2_BIT	27				// Bit on port for LED2

#define LED_ON 		1				// Level to set port to turn on led
#define LED_OFF 	0				// Level to set port to turn off led

//
// Private Variables
//
IOCON_PIO_CFG_Type PIO_ConfigStructure;

//
// task parameter
//
typedef struct {
	LPC_GPIO_TypeDef* port;
	uint8_t bitPosi;
	uint16_t period;
}LED_T;

LED_T tskLED1 = {LED1_PORT, LED1_BIT, 200};
LED_T tskLED2 = {LED2_PORT, LED2_BIT, 500};

//
// LED1 Task
//
void vTask_LED(void *pvParameters) {
	LED_T *led = (LED_T *) pvParameters;
	while(1) {
		GPIO_SetInvert(led->port, led->bitPosi, 1);
		vTaskDelay(led->period/portTICK_RATE_MS);
	}
}

int main(void) {

	// set GPIO
	IOCON_PIO_CFG_Type PIO_mode;
	SYS_ConfigAHBCLK(SYS_AHBCLKCTRL_GPIO0, ENABLE);
	IOCON_StructInit(&PIO_mode);

	PIO_mode.type = IOCON_PIO_0_7;
	IOCON_SetFunc(&PIO_mode);

	PIO_mode.type = IOCON_PIO_0_27;
	IOCON_SetFunc(&PIO_mode);

	GPIO_SetDir(LED1_PORT, LED1_BIT, 1);
	GPIO_SetDir(LED2_PORT, LED2_BIT, 1);


	// Start LED1 Task
	xTaskCreate(
			vTask_LED,					// task function
			(signed char *) "led1",		// task name
			configMINIMAL_STACK_SIZE,	// task stack size
			&tskLED1,					// task parameters
			tskIDLE_PRIORITY,			// task priority
			NULL);						// task handle

	xTaskCreate(
			vTask_LED,					// task function
			(signed char *) "led2",		// task name
			configMINIMAL_STACK_SIZE,	// task stack size
			&tskLED2,					// task parameters
			tskIDLE_PRIORITY,			// task priority
			NULL);						// task handle

	// start the tasks and timer running
	vTaskStartScheduler();

	return 0 ;
}

//
// FreeRTOS Hook Functions
//
void vApplicationIdleHook() {
	// TODO: FreeRTOS Idle Hook
}

void vApplicationTickHook() {
	// TODO: FreeRTOS Tick Hook
}

void vApplicationMallocFailedHook() {
	// TODO: Faile malloc Hook
}
